#!/usr/bin/env python
from .cache import cache


class IsCached:
    def __init__(self, key, expire=86400):
        self._key = key
        self._expire = expire

    def __call__(self, id):
        key = self._key % id
        if cache.get(key):
            return 1
        return 0

    def __lshift__(self, id):
        key = self._key % id
        cache.setex(key, self._expire, 1)

    def clear(self):
        for i in cache.keys(self._key % "*"):
            cache.delete(i)


if __name__ == "__main__":
    is_cached = IsCached("CacheSetexTest:%s", 3)
    id = 32232
    print(is_cached(id))
    is_cached << id
    print(is_cached(id))
    is_cached.clear()
    print(is_cached(id))
